#ifndef GAME_CONTROLLER_H
#define GAME_CONTROLLER_H

#include <qsplashscreen.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <windows.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <time.h>

#include <common.h>
#include <game_model.h>
#include <mainwindow.h>
#include <player_profile.h>

#include <forms/newgamedialog.h>
#include <forms/logindialog.h>
#include <forms/newuserdialog.h>
#include <forms/aboutdialog.h>


struct Move;
class GameWindow;

//
//
// TODO: game saving!
//
//

class GameController;

class Game {
public:
	Game (GameController * gc); 
	~Game () ;
	int getPlayersCount () const; 
	int getCurrentPlayer () const;
	void setCurrentPlayer (int i);
	int nextPlayer ();
	void removePlayer (int i);
	PlayerProfile * getPlayer (int i) const ;
	DotsGameModel * getModel () const;
	int addPlayer (PlayerProfile * p);
	void reset ();
	time_t getTimeStart () const;
protected:
	std::vector<PlayerProfile *> players;
	DotsGameModel * model;
	int currentPlayer;
	GameController * controller;
	time_t startTime;
};

class GameMoveEvent : public QEvent {
public:
	GameMoveEvent(Point _p);
	~GameMoveEvent();

	Type type () const; 
	Point point () const; 

protected:
	Point _point;

};

class GameController : public QObject, public EventReceiver {
	Q_OBJECT

public:
	GameWindow * gw;

	GameController ();
	~GameController ();

	void exec ();
	Game * getCurrentGame () const;
	void moveEvent (GameMoveEvent * e);
	void push (Event * e); //events from GameModel

public slots:
	// From main window
    virtual void gameNew();
    virtual void gameEndForCurrent();
    virtual void gameEndForAll();
    virtual void gameReset();
    virtual void gameSettings();
	virtual void gameGlobalSettings();
	virtual void gameLogout();
    virtual void gameExit();
    virtual void infoRating();
    virtual void helpContents();
	virtual void helpRules();
	virtual void helpAbout();
	//From login dialog
	virtual void loginIconChange (QIconViewItem * item);
	virtual void loginNewUser ();
	virtual bool loginEnter ();

	virtual void loginDialogUpdate ();

	//From new-user dialog
	virtual void newUserChooseIcon ();
	virtual void newUserDefaultIcon ();
	virtual void newUserChooseColor (const QString & string);
	//From new-game dialog
	virtual void newGamePlayer1Change (const QString & string);
	virtual void newGamePlayer2Change (const QString & string);
	virtual void newGameNewPlayer1 ();
	virtual void newGameNewPlayer2 ();
	virtual void newGameChangeColorPlayer1 ();
	virtual void newGameChangeColorPlayer2 ();

	//Rating buttons
	virtual void ratingSave ();
	virtual void ratingLoad ();
	virtual void ratingClose ();
	virtual void ratingExport ();


	int getFieldWidth ();
	int getFieldHeight ();

/* !!!!!!!!!!!
TODO:
create dialog directors (mediator pattern)
*/

protected:
	//
	typedef std::vector<PlayerProfile *> PlayerProfilesList;

	//
	//bool silent;
	//bool loggedIn;

	// view-side
	
	LoginDialog * loginDialog;
	NewUserDialog * newUserDialog;
	NewGameDialog * newGameDialog;
	AboutDialog * aboutDialog;
	std::stack<QWidget *> viewStack;
	PlayerProfilesList profiles;

	// model-side
	Game * currentGame;
	PlayerProfile * currentProfile;
	
protected: // methods

	void initConnections ();

	QWidget * showPreviousView ();
	void showNewView (QWidget * w);
	
	void readProfiles (char * fileName);
	void saveProfiles (char * fileName);

	PlayerProfile * findProfile (QString _name, QString _passwd, bool _ignorePasswd = false);
	PlayerProfile * createNewProfile ();

	static bool ratingCmpFunc (const RatingItem & i1, const RatingItem & i2);
	void updateRating (); //update rating using current game

	
	void setFieldSize (int x, int y);

};



#endif /* !GAME_CONTROLLER_H */

